﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.DALFactory;
using OgilvyOne.IDAL;
using OgilvyOne.Model;
using OgilvyOne.Utility;

namespace OgilvyOne.BLL
{
    public class ColumnBll
    {
        private static IList columnlist; //栏目列表
        private static bool isupdate = true;

        public const string COLUMN_LAYERID = "001";
        /// <summary>
        /// 是否有缓存或更新状态
        /// </summary>
        /// <returns></returns>
        public static bool IsUpdate()
        {
            return columnlist == null || isupdate;
        }

        /// <summary>
        /// 获取缓存中所有栏目列表
        /// </summary>
        /// <returns></returns>
        public static IList GetColumnList()
        {
            if (columnlist == null || isupdate == true)
            {
                columnlist = ColumnFactory.create().getColumnAllList();
                isupdate = false;
            }
            return columnlist;
        }

        /// <summary>
        /// 获取子栏目列表
        /// </summary>
        /// <param name="pcid">父ID</param>
        /// <returns></returns>
        public IList GetColumnList(int pcid)
        {
            return ColumnFactory.create().getColumnList(pcid);
        }

        /// <summary>
        /// 新增栏目
        /// </summary>
        /// <param name="column"></param>
        public bool AddColumn(ColumnModel column)
        {
            isupdate = true;
            return ColumnFactory.create().addColumn(column);
        }
        /// <summary>
        /// 修改栏目信息
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public bool ModColumn(ColumnModel column)
        {
            isupdate = true;

            bool re = ColumnFactory.create().modColumn(column);
            return re;
        }



        /// <summary>
        /// 删除栏目信息
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public bool DelColumn(int cid)
        {
            isupdate = true;
            return ColumnFactory.create().delColumn(cid);
        }

        //private static DateTime modtime ;
        /// <summary>
        /// 修改栏目顺序
        /// </summary>
        /// <param name="cid">栏目</param>
        /// <param name="isF">上或下移 (1上,0下)</param>
        /// <returns>false 排序修改失败,不可频繁修改</returns>
        public bool ModColumnOrderby(int cid, int isF)
        {
            //if (modtime == null) modtime = DateTime.Now;
            //if (DateTime.Now.Ticks - modtime.Ticks > 2000)
            {
                IColumn ic = ColumnFactory.create();
                ColumnModel cm = ic.getColumn(cid);
                IList clist = ic.getColumnList(cm.Parentid);
                int maxorderid = ic.MaxOrder(cm.Parentid);
                if (clist.Count != maxorderid)
                {//将所有按序排
                    for (int i = 0; i < clist.Count; i++)
                    {
                        ColumnModel clcm = (ColumnModel)clist[i];
                        ic.ModOrderColumn(clcm.Classid, i + 1);
                    }
                }
                if (isF == 0)
                {//上移
                    cm = ic.getColumn(cid);
                    int cmorderid = cm.Orderid - 1;
                    int peid = ic.OrderColumn(cm.Parentid, cmorderid);
                    ic.ModOrderColumn(cid, cmorderid);
                    ic.ModOrderColumn(peid, cmorderid + 1);
                }
                else
                {//下移
                    cm = ic.getColumn(cid);
                    int cmorderid = cm.Orderid + 1;
                    int peid = ic.OrderColumn(cm.Parentid, cmorderid);
                    ic.ModOrderColumn(cid, cmorderid);
                    ic.ModOrderColumn(peid, cmorderid - 1);
                }
                isupdate = true;
                return true;
            }
            
            //return false;
        }
        /// <summary>
        /// 单个栏目信息
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ColumnModel GetColumn(int cid)
        {
            return ColumnFactory.create().getColumn(cid);
        }

        /// <summary>
        /// 子栏目最大排序值
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public int MaxOrder(int pid)
        {
            return ColumnFactory.create().MaxOrder(pid);
        }
        /// <summary>
        /// 栏目管理,层级导航
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public string LayeridColumnNaiv(string layerid)
        {
            if (layerid == null) return "";
            IList clist = ColumnFactory.create().getColumnLayerList(layerid);
            string html = "当前栏目位置: ";
            
            for(int i=0;i<clist.Count;i++){
                UserColumnModel uc = (UserColumnModel)clist[i];
                if (i > 0)
                {
                    html += "<img src='../images/subnav_dot.gif' width='9' height='9' />" ;
                }
                html += "&nbsp;&nbsp;<a href='column_list.aspx?columnid=" + uc.Columnid + "&layerid="+ uc.Layerid +"'>" + uc.Columnname + "</a>&nbsp;&nbsp;";
            }
            return html;
        }

        /// <summary>
        /// 栏目静态发布管理,层级导航
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public string StaticPubLayeridColumnNaiv(string layerid, string playerid)
        {
            if (layerid == null) return "";
            IList clist = ColumnFactory.create().getColumnLayerList(layerid);
            string html = "";
            int webid = 2;
            for (int i = 0; i < clist.Count; i++)
            {
                UserColumnModel uc = (UserColumnModel)clist[i];
                if (i > 0)
                {
                    html += "<img src='../images/subnav_dot.gif' width='9' height='9' />";
                }
                if (i == 0) webid = uc.Columnid;
                html += "&nbsp;&nbsp;<a href='static_pub_list.aspx?columnid=" + uc.Columnid + "&layerid=" + playerid + "&webid=" + webid + "'>" + uc.Columnname + "</a>&nbsp;&nbsp;";
            }
            return html;
        }

        /// <summary>
        /// 内容管理,层级导航
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public string LayeridColumnConNaiv(string layerid)
        {
            if (layerid == null) return "";
            IList clist = ColumnFactory.create().getColumnLayerList(layerid);
            string html = "当前位置: ";
            string path = System.Web.HttpContext.Current.Request.ApplicationPath;
            if (path == "/")
            {
                path = "";
            }

            path += "/admin/";

            for (int i = 0; i < clist.Count; i++)
            {
                UserColumnModel uc = (UserColumnModel)clist[i];
                if (i > 0)
                {
                    html += "<img src='" + path + "images/subnav_dot.gif' width='9' height='9' />";
                }
                string url = uc.Columnurl;
                if (url != null && url != "0" && url.Trim() != "")
                {
                    if (url.IndexOf("?") > -1)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }
                    url = url.Replace("../", "");
                    url += "columnid=" + uc.Columnid + "&layerid=" + uc.Layerid;
                    html += "&nbsp;&nbsp;<a href='" + path + url + "'>" + uc.Columnname + "</a>&nbsp;&nbsp;";
                }
                else
                {
                    html += "&nbsp;&nbsp;" + uc.Columnname + "&nbsp;&nbsp;"; ;
                }
            }
            return html;
        }

        /// <summary>
        /// 系统管理,层级导航
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public string LayeridSysColumnNaiv(string layerid)
        {
            if (layerid == null) return "";
            IList clist = ColumnFactory.create().getSysColumnLayerList(layerid);
            string html = "当前位置: ";

            for (int i = 0; i < clist.Count; i++)
            {
                UserColumnModel uc = (UserColumnModel)clist[i];
                if (i > 0)
                {
                    html += "<img src='../images/subnav_dot.gif' width='9' height='9' />";
                }
                string url = uc.Columnurl;
                if (url != null&& url != "0")
                {
                    if (url.IndexOf("?") > -1)
                    {
                        url += "&";
                    }
                    else
                    {
                        url += "?";
                    }
                    url += "columnid=" + uc.Columnid + "&layerid=" + uc.Layerid;
                    html += "&nbsp;&nbsp;<a href='" + url + "'>" + uc.Columnname + "</a>&nbsp;&nbsp;";
                }
                else
                {
                    html += "&nbsp;&nbsp;"+ uc.Columnname + "&nbsp;&nbsp;";
                }

                
                
            }
            return html;
        }

        /// <summary>
        /// 获取HTML模板标签列表
        /// </summary>
        /// <param name="layerid"></param>
        /// <returns></returns>
        public IList getHtmlTempLabelList()
        {
            return ColumnFactory.create().getHtmlTempLabelList();
        }

        public IList GetColumnAllList()
        {
            return ColumnFactory.create().getColumnAllList();
        }

        ////--------------------------------------------提示7日示发布数据
        /// <summary>
        /// 栏目文章正稿汇总
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public IList GetArticleInitGroup()
        {
            return ColumnFactory.create().GetArticleStateGroup(CmsConstants.STATE_INIT);
        }
        /// <summary>
        /// 栏目文章审核汇总
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public IList GetArticleAuditGroup()
        {
            return ColumnFactory.create().GetArticleStateGroup(CmsConstants.STATE_AUDIT_YES);
        }
        
    }
}
